package com.doyens.gmall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doyens.gmall.common.result.Result;
import com.doyens.gmall.model.product.*;
import com.doyens.gmall.product.service.ManageService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("admin/product")
@Api("商品基础属性接口")
//@CrossOrigin   解决跨域，每一个controller都得加该注解，比较灵活，但是所有controller都加有些麻烦
public class ManageController {
    @Autowired
    ManageService ms;
@GetMapping("getCategory1")
    public Result<List<BaseCategory1>> getBaseCategory1(){
    List<BaseCategory1> category1 = ms.getCategory1();
    return Result.ok(category1);
}

    @GetMapping("getCategory2/{category1Id}")
    public Result<List<BaseCategory2>> getBaseCategory2(@PathVariable("category1Id") Long category1){
        List<BaseCategory2> category2 = ms.getCategory2(category1);
        return Result.ok(category2);
    }

    @GetMapping("getCategory3/{category2Id}")
    public Result<List<BaseCategory3>> getBaseCategory3(@PathVariable("category2Id") Long category2){
        List<BaseCategory3> category3 = ms.getCategory3(category2);
        return Result.ok(category3);
    }

    @GetMapping("attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result<List<BaseAttrInfo>> getBaseAttrInfo(@PathVariable("category1Id") Long category1,@PathVariable("category2Id") Long category2,@PathVariable("category3Id") Long category3){
        List<BaseAttrInfo> attrInfoList = ms.getAttrInfoList(category1, category2, category3);
        return Result.ok(attrInfoList);
    }


    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        System.out.println(baseAttrInfo.getAttrValueList());
    ms.saveAttrInfo(baseAttrInfo);
    return Result.ok();
    }

    @GetMapping("getAttrValueList/{attrId}")
    public Result<List<BaseAttrValue>> findAttrValueListByid(@PathVariable("attrId")Long attrId){
    BaseAttrInfo baseAttrInfo=ms.getBaseAttrInfo(attrId);
    return Result.ok(baseAttrInfo.getAttrValueList());
    }

    @GetMapping("{page}/{limit}")
    public Result getSpuInfoPage(@PathVariable("page") Long page,
                                 @PathVariable("limit") Long limit,
                                 SpuInfo spuInfo){
        //创建一个page对象
        Page<SpuInfo> spuInfoPage = new Page<>(page,limit);
        //获取数据
        IPage<SpuInfo> spuInfoIPage = ms.getSpuInfoPage(spuInfoPage,spuInfo);
        return Result.ok(spuInfoPage);
    }

    @GetMapping("baseSaleAttrList")
    public Result<List<BaseSaleAttr>> getbaseSaleAttrList(){
   List<BaseSaleAttr> f= ms.getbaseSaleAttr();
    return Result.ok(f);
    }

    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
    ms.saveSpuInfo(spuInfo);
    return Result.ok();
    }


}
